package com.ferrlo.web.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.onlineframework.commons.util.MD5Util;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ferrlo.model.User;
import com.ferrlo.service.IUserManager;
import com.ferrlo.servlet.SampleImageCaptchaService;
import com.ferrlo.utils.SMSUtils;
import com.octo.captcha.service.image.ImageCaptchaService;


@Controller
public class RegisterController {
	
	@Resource(name="userManager")
	private IUserManager userManager;


	@RequestMapping(value="/register")
	public String register(Map<String, Object> map,HttpServletRequest request){
		
		ImageCaptchaService service = SampleImageCaptchaService.getInstance();
		
		String username = request.getParameter("username");
		String pwd = MD5Util.getMD5(request.getParameter("pwd"));
		String phone = request.getParameter("number");
		String mail = request.getParameter("email");
		String qq = request.getParameter("numberqq");
		String captcha = request.getParameter("captcha");
		
		System.out.println("=====================");
		System.out.println("CAPTCHA: "+captcha);
		if (service.validateResponseForID(request.getSession(true).getId(),captcha)){
			User user = new User(username, pwd, mail, phone, qq);
			user.setURegTime(new Date());
			saveUser(user);
		}else {
			System.out.println("NO");
		}

		return "index";
	}
	
	@RequestMapping(value="/register.json")
	public Map<String, Object> register_JSON(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();

		ImageCaptchaService service = SampleImageCaptchaService.getInstance();
		
		String username = request.getParameter("username");
		String pwd = MD5Util.getMD5(request.getParameter("pwd"));
		String phone = request.getParameter("number");
		String mail = request.getParameter("email");
		String qq = request.getParameter("numberqq");
		String captcha = request.getParameter("captcha");
		
		if (service.validateResponseForID(request.getSession(true).getId(),captcha)){
			User user = new User(username, pwd, mail, phone, qq);
			user.setURegTime(new Date());
			saveUser(user);
		}else {
			System.out.println("NO");
		}
	
		
		return map;
	}
	
	
	@RequestMapping(value="validatePhone.json")
	@ResponseBody
	public Map<String, Object> validatePhone_JSON(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
	
		String phone = request.getParameter("phone");
		HttpSession session = request.getSession();
		int result = getPhoneVald(phone); 
		session.setAttribute("val_phone", result);

		return map;
	}
	
	

	//private 方法，供 json 和普通 http 封装用
	
	/**
	 * 发送验证码到指定手机
	 * @param phone
	 * @return 发送验证码结果 
	 */
	private int getPhoneVald(String phone){

		double random = Math.random();
		int ranInt = (int) (random*9000+1000);
		String content = "您的验证码是："+ ranInt;

		String result = "fail";
		try {
			result = SMSUtils.Send_SMS(content, phone);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ranInt;
	}
	
	/**
	 * 将用户存到数据库
	 * @param user
	 * @return 是否成功
	 */
	private Boolean saveUser(User user){
		
		userManager.save(user);
		
		return false;
	}
	
}
